﻿// Fill out your copyright notice in the Description page of Project Settings.

#include "SphereBase.h"
#include "Components/StaticMeshComponent.h"
#include "Camera/CameraComponent.h"
#include "GameFramework/SpringArmComponent.h"
#include "Components/InputComponent.h"
#include "Engine.h"


// Sets default values
ASphereBase::ASphereBase()
{
 	// Set this pawn to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = true;

	isInput = true;
	SphereSpeed = 200.0f;
	SphereMinSpeed = SphereSpeed;
	SphereMaxSpeed = 600.0f;

	SphereMeshComp = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("MySphereMesh"));
	//RootComponent = SphereMesh;
	SphereMeshComp->SetSimulatePhysics(true);

	CameraComp = CreateDefaultSubobject<UCameraComponent>(TEXT("MainCamera"));
	CameraArmComp = CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraArm"));

	CameraArmComp->SetupAttachment(SphereMeshComp);
	CameraComp->SetupAttachment(CameraArmComp);
}

// Called when the game starts or when spawned
void ASphereBase::BeginPlay()
{
	Super::BeginPlay();
	
}

// Called every frame
void ASphereBase::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);

	if (isInput && SphereAngularVelocity != FVector(0, 0, 0))
	{
		SphereMeshComp->SetPhysicsAngularVelocity(SphereAngularVelocity);
	}

}

// Called to bind functionality to input
void ASphereBase::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
	Super::SetupPlayerInputComponent(PlayerInputComponent);

	PlayerInputComponent->BindAxis("MoveForward", this, &ASphereBase::MoveForward);
	PlayerInputComponent->BindAxis("MoveRight", this, &ASphereBase::MoveRight);

	PlayerInputComponent->BindAction("SpeedUp", IE_Pressed, this, &ASphereBase::SpeedUp);
	PlayerInputComponent->BindAction("SpeedUp", IE_Released, this, &ASphereBase::SlowDown);
}

void ASphereBase::MoveForward(float val)
{
	if (!isInput)
		return;
	SphereAngularVelocity.Y = SphereSpeed * val;
}

void ASphereBase::MoveRight(float val)
{
	if (!isInput)
		return;
	SphereAngularVelocity.X = SphereSpeed * val;
}

void ASphereBase::SpeedUp()
{
	SphereSpeed = SphereMaxSpeed;
}

void ASphereBase::SlowDown()
{
	SphereSpeed = SphereMinSpeed;
}
